﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.Revit.MEP.Calculator
{
	public class BuildingPlaceDb
	{
		public static BuildingPlaceDb ThisDb
		{
			get
			{
				return BuildingPlaceDb.thisDb;
			}
		}

		private BuildingPlaceDb()
		{
			string dbFile = BuildingPlaceCommon.GetDbFile();
			this.dbAssist = new SQLiteDatabaseAssist(dbFile);
		}

		public List<BuildingPlaceData> GetDatas()
		{
			List<BuildingPlaceData> list = new List<BuildingPlaceData>();
			List<BuildingPlaceData> result;
			try
			{
				string sqlStr = string.Format("select * from {0}", "IllStandard");
				System.Data.DataTable dataTable = this.dbAssist.ExecuteQuery(sqlStr);
				if (dataTable == null)
				{
					result = list;
				}
				else
				{
					foreach (object obj in dataTable.Rows)
					{
						DataRow dataRow = (DataRow)obj;
						list.Add(new BuildingPlaceData
						{
							Id = Convert.ToInt32(dataRow["Id"]),
							Industry = dataRow["Industry"].ToString(),
							BuildingType = dataRow["BuildingType"].ToString(),
							Place = dataRow["Place"].ToString(),
							RefPlane = dataRow["RefPlane"].ToString(),
							NowPD = dataRow["NowPD"].ToString(),
							TargetPD = dataRow["TargetPD"].ToString(),
							IllStandard = Convert.ToInt32(dataRow["IllStandard"]),
							GlareValue = dataRow["GlareValue"].ToString(),
							IllEvenness = dataRow["IllEvenness"].ToString(),
							Ra = Convert.ToInt32(dataRow["Ra"])
						});
					}
					result = list;
				}
			}
			catch (Exception)
			{
				MessageBox.Show("读取照明标准数据失败，请检查数据源。", "盈建科提示");
				result = new List<BuildingPlaceData>();
			}
			return result;
		}

		public BuildingPlaceData GetRoomTypeDataById(string industry, string buildingType, string place, string id)
		{
			BuildingPlaceData buildingPlaceData = new BuildingPlaceData();
			BuildingPlaceData result;
			try
			{
				string sqlStr = string.Format("select * from {0} where [{1}]='{2}'", "IllStandard", "Industry", "民用");
				System.Data.DataTable dataTable = this.dbAssist.ExecuteQuery(sqlStr);
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = buildingPlaceData;
				}
				else
				{
					DataRow dataRow = dataTable.Rows[0];
					buildingPlaceData.Id = Convert.ToInt32(dataRow["Id"]);
					buildingPlaceData.Industry = dataRow["Industry"].ToString();
					buildingPlaceData.BuildingType = dataRow["BuildingType"].ToString();
					buildingPlaceData.Place = dataRow["Place"].ToString();
					buildingPlaceData.RefPlane = dataRow["RefPlane"].ToString();
					buildingPlaceData.NowPD = dataRow["NowPD"].ToString();
					buildingPlaceData.TargetPD = dataRow["TargetPD"].ToString();
					buildingPlaceData.IllStandard = Convert.ToInt32(dataRow["IllStandard"]);
					buildingPlaceData.GlareValue = dataRow["GlareValue"].ToString();
					buildingPlaceData.IllEvenness = dataRow["IllEvenness"].ToString();
					buildingPlaceData.Ra = Convert.ToInt32(dataRow["Ra"]);
					int num = string.IsNullOrWhiteSpace(id) ? -1 : Convert.ToInt32(id);
					sqlStr = string.Format("select * from {0} where [{1}]={2}", "IllStandard", "Id", num);
					dataTable = this.dbAssist.ExecuteQuery(sqlStr);
					if (dataTable != null && dataTable.Rows.Count > 0)
					{
						dataRow = dataTable.Rows[0];
						buildingPlaceData.Id = Convert.ToInt32(dataRow["Id"]);
						buildingPlaceData.Industry = dataRow["Industry"].ToString();
						buildingPlaceData.BuildingType = dataRow["BuildingType"].ToString();
						buildingPlaceData.Place = dataRow["Place"].ToString();
						buildingPlaceData.RefPlane = dataRow["RefPlane"].ToString();
						buildingPlaceData.NowPD = dataRow["NowPD"].ToString();
						buildingPlaceData.TargetPD = dataRow["TargetPD"].ToString();
						buildingPlaceData.IllStandard = Convert.ToInt32(dataRow["IllStandard"]);
						buildingPlaceData.GlareValue = dataRow["GlareValue"].ToString();
						buildingPlaceData.IllEvenness = dataRow["IllEvenness"].ToString();
						buildingPlaceData.Ra = Convert.ToInt32(dataRow["Ra"]);
					}
					else
					{
						if (string.IsNullOrWhiteSpace(industry) || string.IsNullOrWhiteSpace(buildingType) || string.IsNullOrWhiteSpace(place))
						{
							return null;
						}
						sqlStr = string.Format("select * from {0} where [{1}]='{2}' and [{3}]='{4}' and [{5}]='{6}'", new object[]
						{
							"IllStandard",
							"Industry",
							industry,
							"BuildingType",
							buildingType,
							"Place",
							place
						});
						dataTable = this.dbAssist.ExecuteQuery(sqlStr);
						if (dataTable == null || dataTable.Rows.Count <= 0)
						{
							return null;
						}
						dataRow = dataTable.Rows[0];
						buildingPlaceData.Id = Convert.ToInt32(dataRow["Id"]);
						buildingPlaceData.Industry = dataRow["Industry"].ToString();
						buildingPlaceData.BuildingType = dataRow["BuildingType"].ToString();
						buildingPlaceData.Place = dataRow["Place"].ToString();
						buildingPlaceData.RefPlane = dataRow["RefPlane"].ToString();
						buildingPlaceData.NowPD = dataRow["NowPD"].ToString();
						buildingPlaceData.TargetPD = dataRow["TargetPD"].ToString();
						buildingPlaceData.IllStandard = Convert.ToInt32(dataRow["IllStandard"]);
						buildingPlaceData.GlareValue = dataRow["GlareValue"].ToString();
						buildingPlaceData.IllEvenness = dataRow["IllEvenness"].ToString();
						buildingPlaceData.Ra = Convert.ToInt32(dataRow["Ra"]);
					}
					result = buildingPlaceData;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public void SaveDatas(List<BuildingPlaceData> dataList)
		{
			if (dataList == null || dataList.Count <= 0)
			{
				return;
			}
			try
			{
				this.dbAssist.BeginTransaction();
				foreach (BuildingPlaceData buildingPlaceData in dataList)
				{
					if ((buildingPlaceData.State & DataState.New) == DataState.None && buildingPlaceData.Id > 0 && (buildingPlaceData.State & DataState.Deleted) != DataState.None)
					{
						string sqlStr = string.Format("delete from {0} where [{1}]={2}", "IllStandard", "Id", buildingPlaceData.Id);
						this.dbAssist.ExecuteCommandEx(sqlStr);
					}
				}
				foreach (BuildingPlaceData buildingPlaceData2 in dataList)
				{
					if ((buildingPlaceData2.State & DataState.New) == DataState.None && buildingPlaceData2.Id > 0 && (buildingPlaceData2.State & DataState.Deleted) == DataState.None && (buildingPlaceData2.State & DataState.Modified) != DataState.None && !string.IsNullOrWhiteSpace(buildingPlaceData2.Place))
					{
						string text = string.Format("update {0} set [{1}]='{2}',[{3}]='{4}',", new object[]
						{
							"IllStandard",
							"Place",
							buildingPlaceData2.Place,
							"RefPlane",
							buildingPlaceData2.RefPlane
						});
						text += string.Format("[{0}]='{1}',[{2}]='{3}',[{4}]={5},", new object[]
						{
							"NowPD",
							buildingPlaceData2.NowPD,
							"TargetPD",
							buildingPlaceData2.TargetPD,
							"IllStandard",
							buildingPlaceData2.IllStandard
						});
						text += string.Format("[{0}]='{1}',[{2}]='{3}',[{4}]={5} ", new object[]
						{
							"GlareValue",
							buildingPlaceData2.GlareValue,
							"IllEvenness",
							buildingPlaceData2.IllEvenness,
							"Ra",
							buildingPlaceData2.Ra
						});
						text += string.Format("where [{0}]={1}", "Id", buildingPlaceData2.Id);
						this.dbAssist.ExecuteCommandEx(text);
					}
				}
				foreach (BuildingPlaceData buildingPlaceData3 in dataList)
				{
					if ((buildingPlaceData3.State & DataState.Deleted) == DataState.None && (buildingPlaceData3.State & DataState.New) != DataState.None && !string.IsNullOrWhiteSpace(buildingPlaceData3.Place))
					{
						string text2 = string.Format("insert into {0} ([{1}],[{2}],[{3}],[{4}],[{5}],", new object[]
						{
							"IllStandard",
							"Industry",
							"BuildingType",
							"Place",
							"RefPlane",
							"NowPD"
						});
						text2 += string.Format("[{0}],[{1}],[{2}],[{3}],[{4}]) ", new object[]
						{
							"TargetPD",
							"IllStandard",
							"GlareValue",
							"IllEvenness",
							"Ra"
						});
						text2 += string.Format("values('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}','{8}',{9})", new object[]
						{
							buildingPlaceData3.Industry,
							buildingPlaceData3.BuildingType,
							buildingPlaceData3.Place,
							buildingPlaceData3.RefPlane,
							buildingPlaceData3.NowPD,
							buildingPlaceData3.TargetPD,
							buildingPlaceData3.IllStandard,
							buildingPlaceData3.GlareValue,
							buildingPlaceData3.IllEvenness,
							buildingPlaceData3.Ra
						});
						this.dbAssist.ExecuteCommandEx(text2);
						text2 = string.Format("select [{0}] from {1} where [{2}]='{3}' and [{4}]='{5}' and [{6}]='{7}'", new object[]
						{
							"Id",
							"IllStandard",
							"Industry",
							buildingPlaceData3.Industry,
							"BuildingType",
							buildingPlaceData3.BuildingType,
							"Place",
							buildingPlaceData3.Place
						});
						int id;
						this.dbAssist.ExecuteScalar(text2, out id);
						buildingPlaceData3.Id = id;
					}
				}
				this.dbAssist.CommitTransaction();
			}
			catch (Exception)
			{
				MessageBox.Show("保存电缆数据失败，请检查数据源。", "盈建科提示");
				this.dbAssist.RollbackTransaction();
			}
		}

		private static BuildingPlaceDb thisDb = new BuildingPlaceDb();

		private SQLiteDatabaseAssist dbAssist;
	}
}
